<?php
/**
 * @copyright  meila.com
 * @author     gaojian3@
 * @name       抓取电商商品数据
 * @param 
 *         $url      =     商品地址
 *         $shop_arr =     电商列表
 *         $preg_arr =     商品URl正则规则
 */
include('../__global.php');
class fetch extends ml_controller{
    private $url;
    private $preg_arr;
    private $shop_arr;
    
    //参数初始化
    function initParam()
    {
        $ip_visit = Tool_ip::get_real_ip();

        
        $this->url = $this->input('url' , 'post');
//        $this->url='http://item.taobao.com/item.htm?spm=110-1m9S-2a0oM.6rRW-4ExOU.v-F5Zxg&id=14352860071';
//    $this->url='http://item.taobao.com/item.htm?id=12518336551';    
//        $this->url='http://item.taobao.com/item.htm?id=15246260925&cm_cat=50012027';
//        $this->url='http://detail.tmall.com/item.htm?id=10182519116&spm=1003.1.1000008.8&scm=1003.649.1123.0';
//$this->url='http://detail.tmall.com/item.htm?id=12354053999&prt=1330134734377&prc=1';
//$this->url='http://item.taobao.com/item.htm?id=12354053999';
//$this->url='http://product.m18.com/p-JK190137.htm';
//        $this->url='http://detail.tmall.com/venus/spu_detail.htm?spu_id=129814445&no_switch=1&default_item_id=10182519116';
//        $this->url='http://item.vancl.com/0170518.html?ref=ch_chenshan_hp_2';
//        $this->url='item.vancl.com/0125236.html?ref=zhuanti_6159_0125236';
//        $this->url='product.m18.com/p-7113067.htm';
        $arr=ml_factory::load_standard_conf('fetch');
        $this->preg_arr=$arr['preg_arr'];
        $this->shop_arr=$arr['shop_arr'];
    }
    
    function checkParam()
    {
        ml_tool_sendMq::send_inter_mail(20, "fetch");
        //判断url错误
        $url_preg= ML_DATARULE_BASE_URL;
        if(!preg_match($url_preg,$this->url)){
            $this->api_output(ML_RCODE_PARAM , 'a');
        }
    }
    
    /**
     * @develop_template
     * curl选项设置（ 替换curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); ）
     */
    private function curl_redir_exec($ch,$debug="") 
    { 
        static $curl_loops = 0; 
        static $curl_max_loops = 20; 
    
        if ($curl_loops++ >= $curl_max_loops) 
        { 
            $curl_loops = 0; 
            return FALSE; 
        } 
        curl_setopt($ch, CURLOPT_HEADER, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        $data = curl_exec($ch); 
        $debbbb = $data; 
        list($header, $data) = explode("\n\n", $data, 2); 
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    
        if ($http_code == 301 || $http_code == 302) { 
            $matches = array(); 
            preg_match('/Location:(.*?)\n/', $header, $matches); 
            $url = @parse_url(trim(array_pop($matches))); 
            //print_r($url); 
            if (!$url) 
            { 
                $curl_loops = 0; 
                return $data; 
            } 
            $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));  
        
            $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:''); 
            curl_setopt($ch, CURLOPT_URL, $new_url); 
            
        //    debug('Redirecting to', $new_url); 
    
            return $this->curl_redir_exec($ch); 
        } else { 
            $curl_loops=0; 
            return $debbbb; 
        } 
    } 
    
    
    //抓取整个页面
    private function get_page()
    {
        try{
            $st = time();

            $ch = curl_init();


            
            curl_setopt($ch,CURLOPT_URL,$this->url);
            curl_setopt($ch,CURLOPT_HEADER,0);
        //    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
            $this->curl_redir_exec($ch);
        //    curl_setopt($ch,CURLOPT_REFERER,true);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_TIMEOUT,15);
            curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)");
            $info['content'] = curl_exec($ch);
            $info['info']=curl_getinfo($ch);
            curl_close($ch);

            $t = time() - $st;
Tool_logger::debugLog('fetch_time' , $t , true);

            return $info;
        }catch(Exception $e){
           return false;
        }
    }
    
    
    
    /**
     * @develop_template
     * 核心业务逻辑
     */
    public function main()
    {
        //url中出现#标识位置，后面的全部去除
        $leng=strpos($this->url,'#');
        if( $leng!==false ){    
            $this->url=substr($this->url,0,$leng);
        }
        
        //获取整个页面
        $info= $this->get_page($this->url);
        $content=$info['content'];
        if(strlen($content) == 0 ){
            Tool_logger::monitorLog('fetch' , 'get_page_err '.$this->url , Tool_logger::LOG_LEVEL_ERROR);
            $this->api_output(ML_RCODE_BUSY);
        }
        $this->url=$info['info']['url'];
        $data = array();

        //查找电商
        $shop = '';
        $host=array('com','cn','net','hk','org','cc','mobi','info','so','co');
        $url_arr=parse_url($this->url);
        $host_arr=explode('.',$url_arr['host']);
        while ($a=array_pop($host_arr)){
            if(!in_array($a,$host)){
                $shop=$a;
                break;
            }
        }
        
        switch($shop){
            case 'ing2ing':
            case 'rutisher':
            case 'suorang':
            case 'baoyeah':
            case 'clafield':
            case 'qjherb':
                $shop='moonbasa';
                break;
            case 'shishang71':
                $shop='shishangqiyi';
                break;
            case 'qq':
                $shop='paipai';
                break;
        }
        
        
        If(!is_array($this->shop_arr[$shop])){
            $this->api_output(ML_RCODE_PARAM , 'b');
        }
                
        //转换编码
        if($this->preg_arr[$shop]['charset'] == 'gbk'){
            $content = Tool_string::gb2utf($content);
        }
        
        
        //获取title
        preg_match($this->preg_arr[$shop]['title'],$content,$title_matches);
        if(empty($title_matches[1])){
            $title_matches[1]=$title_matches[2];
        }
        $preg_a='|>?\s*([^<>]+)\s*<|';
        if(preg_match($preg_a,$title_matches[1],$title)){
            $gd_title = $title[1];
        }else{
            $gd_title = $title_matches[1];
        }
        $data['gd_title'] = trim(strip_tags($gd_title));
        //将html实体转换成utf-8
        if( preg_match('/&#([0-9]{1,5});/',$data['gd_title']) ){
            $data['gd_title']=mb_convert_encoding(mb_convert_encoding($data['gd_title'] ,'HTML-ENTITIES','UTF-8'),'UTF-8','HTML-ENTITIES');
        }
        
        //获取pic
        preg_match($this->preg_arr[$shop]['img'] , $content , $img_matches);
        
        $data['gd_small_pic']=$img_matches[1];
        
        //增加shop的话，搜索也要更新，请上线前及时告知@huhong
        switch ($shop){
            case 'taobao':
            case 'tmall':
                $gd_pic=preg_replace('/_\d+x\d+.jpg/','',$img_matches[1]);break;
                
            case 'vancl':
                $gd_pic=str_replace("/mid/","/big/",$img_matches[1]);break;
            case 'm18':
                $gd_pic=str_replace('img2.m18.com/GOODS/LARGE/','img.m18.com/GOODS/BIG/',$img_matches[1]);break;
            case 'paipai':
                $gd_pic=str_replace('.300x300',"",$img_matches[1]);break;
            case 'moonbasa':
                $gd_pic=str_replace("/Small/","/Huge/",$img_matches[1]);
                $gd_pic=str_replace("S01.jpg","H01.jpg",$gd_pic);
                break;
            case 'mbaobao':
                $gd_pic=str_replace("/480/","/1024/",$img_matches[1]);
                break;
            case 'shishangqiyi':
                $gd_pic=str_replace("_m","_l",$img_matches[1]);
                break;
            case 'sephora':
                $gd_pic = $this->shop_arr[$shop]['url'].$data['gd_small_pic'];
                $result = file_get_contents($gd_pic);
                if (empty($result)){
                    $gd_pic = $this->shop_arr[$shop]['url'].$img_matches[2];
                }
                $data['gd_small_pic'] = $gd_pic;
                $img_matches[1] = null;
                break;
            default:
                $gd_pic=$img_matches[1];
        }
        $headinfo = @get_headers($gd_pic,1);
        if(!preg_match('/200/',$headinfo[0])){
            $gd_pic=$img_matches[1];
        }
        $data['gd_pic'] = trim($gd_pic);
        
        //获取price
        preg_match($this->preg_arr[$shop]['price'] , $content , $price_matches);
        
        if(strpos($price_matches[1],'-')===false){
            $data['gd_price']= trim($price_matches[1]);
        }else{
            $arr=explode('-',$price_matches[1]);
            $data['gd_price']=trim(array_pop($arr));
        }
        if(isset($price_matches[2])){    //好乐买，不好玩，还得让我减差价
            $data['gd_price']=$price_matches[1]-$price_matches[2];
        }
        
        $data['gd_shop']= $shop;
        $data['gd_url']= $this->url;

        foreach($data as $key => $val ){
            if(empty($val))
            {
                Tool_logger::monitorLog('fetch' , 'field_empty '.$key , Tool_logger::LOG_LEVEL_ERROR);
                $this->api_output(ML_RCODE_PARAM , 'c');
            }
        }
        
        $this->api_output(ML_RCODE_SUCC,$data);
    }
}
new fetch();
        